<?php

namespace Common\Model;

use Think\Model;

/*
 * 手机问题描述表
 */

class PhoneDescModel extends Model {

    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
    );  // 自动完成定义

    /*
     * 获取手机问题描述列表
     */

    public function getPhoneDescList($pageNo = 1, $pageSize = 15, $where = array(), $order = array(), $field = "*") {
        $rt = $this->field($field)->where($where)->page("$pageNo,$pageSize")->order($order)->select();
        return $rt;
    }

    /*
     * 获取手机机型总数
     */

    public function getPhoneListLength($where = array()) {
        $result = $this->where($where)->count();
        return $result;
    }

    /*
     * 添加
     */

    public function addModel($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    /*
     * 获取对应手机品牌型号组合的问题(前台调用)
     */

    public function getDescComb($modelID) {
        $where = array(
            "a.modelID" => $modelID,
        	"a.isOn"=>1
        );

        $rt = M("PhoneDescComb a")
                ->join(C("DB_PREFIX") . "phone_desc b on a.descID=b.descID")
                ->where($where)
                ->order("a.descID asc")
                ->select();

        return $rt;
    }

    /*
     * 获取ID对应的所有子ID
     */

    public function getChildsIDs($pid) {
        $tempArr = $this->where("pid = $pid")->getField('descID',TRUE);
        return $tempArr;
    }
    
    
    /**
     * 删除(批量)
     */
    public function deleteDesc($ids) {
        $data = array("isOn" => 0, "updateTime" => time());
        return $this->where("descID in ( $ids )")->save($data);
    }

    /*
     *删除单条 
     */
    public function deleteDescSingle($id){
        $where=array(
            "descID"=>$id
        );
        $data = array("isOn" => 0, "updateTime" => time());
        return $this->where($where)->save($data);
    }
    
    
     /**
     * 更新信息(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     * @return type
     */
    public function updateDesc($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;

    }
    
     /*获取品牌ID*/
    public function getBrandID($modelID){
       $brandID = $this->where("modelID = $modelID")->getField('brandID');
       return $brandID;
    } 
    
    /*获取问题列表数据*/
    public function getDesc(){
        $res = $this->where("isOn = 1")->field('descID,desc,level,pid')->select();
        return $res;
    }
}
